package fr.miage.ageco.web.beans;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;
import org.primefaces.event.RowEditEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;

import fr.miage.ageco.dataaccess.bo.Competence;
import fr.miage.ageco.dataaccess.services.CompetenceService;

@ViewScoped
@ManagedBean
public class CompetenceBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6918494880259016913L;
	
	private Logger logger = Logger.getLogger(CompetenceBean.class); 
	
	CompetenceService competenceService;

	private List<Competence> listCompetence;
	private List<Competence> filteredCompetence;
	private Competence selectedCompetence;
	

	private int id;
	private String nom;

	public CompetenceBean() {

	}

	@PostConstruct
	public void init() {
		listCompetence = getAllCompetence();
	}

	// Méthodes pour l'IHM
	
	public List<Competence> getAllCompetence() {
		return competenceService.getAllCompetences();
	}


	public List<Competence>  getCompetenceForEmploye(int idEmploye) {
		if(idEmploye != 0){
		logger.info("fonction getcompetenceforemploye : id employe"+ idEmploye);
		return competenceService.getCompetenceForEmploye(idEmploye);
		}
		return null;
	}

	// Ajout
	public String insertCompetence() {

		Competence competence = new Competence();

		// getNom() pour récupérer le nom saisi par l'utilisateur dans le champ
		competence.setNom(getNom());

		// Ajout dans la BD
		competenceService.insertCompetence(competence);

		// Rechargement de la liste
		listCompetence = getAllCompetence();
		
		FacesMessage msg = new FacesMessage("Compétence ajoutée : ",
				(competence.getNom()));
		FacesContext.getCurrentInstance().addMessage(null, msg);
		return "";
	}
	
	public String insertCompetenceForEmploye(int idEmploye, int idCompetence) {
		logger.info("idemploye" + idEmploye + "idcompetence "+idCompetence);
		competenceService.insertCompetenceForEmploye(idCompetence, idEmploye);
		
		FacesMessage msg = new FacesMessage("Compétence ajoutée à l'employé.");
		FacesContext.getCurrentInstance().addMessage(null, msg);
		return "";
	}

	
	/*public void onEdit(RowEditEvent event) {  
        FacesMessage msg = new FacesMessage("Competence edité");  
  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }  
      
    public void onCancel(RowEditEvent event) {  
        FacesMessage msg = new FacesMessage("Competence annulé");  
  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }*/

	// Modification
	public void onEdit(RowEditEvent event) {
		FacesMessage msg = new FacesMessage("Compétence éditée :",
				((Competence) event.getObject()).getId() + "");
		// Modification de l'objet en base
		competenceService.updateCompetence((Competence) (event.getObject()));
		// Rechargement de la liste
		listCompetence = getAllCompetence();
		
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

	// suppression
	public void onCancel(RowEditEvent event) {
		FacesMessage msg = new FacesMessage("Compétence supprimée :",
				((Competence) event.getObject()).getId()+"");
		//suppression dans le tableau
		listCompetence.remove((Competence) event.getObject());
		//suppression en base
		competenceService.deleteCompetence((Competence) event.getObject());
		//Affichage du message
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}
	
	
	// Sélection d'une ligne
	public void onRowSelect(SelectEvent event) {  
        FacesMessage msg = new FacesMessage("Compétence ", ((Competence) event.getObject()).getId() + " sélectionnée"); 
       // selectedCompetence = (Competence) event.getObject();
  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }  
	
	// Déselection de la ligne
    public void onRowUnselect(UnselectEvent event) {  
        FacesMessage msg = new FacesMessage("Compétence ", ((Competence) event.getObject()).getId() + " desélectionnée");  
  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }  

	// Getters & setters

	public String getNom() {
		return nom;
	}

	public CompetenceService getCompetenceService() {
		return competenceService;
	}

	public void setCompetenceService(CompetenceService competenceService) {
		this.competenceService = competenceService;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public List<Competence> getListCompetence() {
		return listCompetence;
	}

	public void setListCompetence(List<Competence> listCompetence) {
		this.listCompetence = listCompetence;
	}

	public List<Competence> getFilteredCompetence() {
		return filteredCompetence;
	}

	public void setFilteredCompetence(List<Competence> filteredCompetence) {
		this.filteredCompetence = filteredCompetence;
	}

	public Competence getSelectedCompetence() {
		return selectedCompetence;
	}

	public void setSelectedCompetence(Competence selectedCompetence) {
		this.selectedCompetence = selectedCompetence;
	}

}
